<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:da="livcos.org/ns/cosmos/data-access"
  xmlns:cb="livcos.org/ns/clipboard"
  xmlns:u="livcos.org/ns/cosmos/update"
  xmlns:t="livcos.org/ns/cosmos/type"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:job="livcos.org/ns/process/job"
  exclude-result-prefixes="c cb t da f">

	<xsl:import href="edit.xsl" />
	
	<xsl:param name="u"/>
	<xsl:param name="i"/>
	<xsl:param name="t"/>
	<xsl:param name="s"/>
	
  <xsl:variable name="sessionObjId" select="$jobObject/c:request/@session"/>
	<xsl:variable name="session" select="document(concat($sessionObjId, '.cosmos'))/*"/>
  <xsl:variable name="clipboard" select="$session/cb:clipboard"/>
  
  <xsl:template match="/">
  	<xsl:choose>
  		<xsl:when test="$u">
  			<xsl:variable name="id" select="if ($i and ($i != 'undefined')) then concat('#', $i) else ''"/>
  			<xsl:call-template name="build"/>
  			<u:context ref="{$sessionObjId}" scope="meta">
  				<u:insert select="cb:clipboard">
  					<u:namespace prefix="cb" uri="livcos.org/ns/clipboard" />
  					<!-- TODO 6: proper entry ID (entity ID are not managed for the Clipboard object ({new-id()} won't work)) -->
  					<cb:entry id="entry_{count($clipboard/cb:entry) + 1}" type="copy" time="{current-dateTime()}">
  						<doc:citation id="citation_1" ref="{$u}{$id}" time="{current-dateTime()}">
  							<xsl:if test="$t"><doc:title><xsl:value-of select="$t"/></doc:title></xsl:if>
  							<xsl:if test="$s"><doc:content><xsl:value-of select="$s"/></doc:content></xsl:if>
  						</doc:citation>
  					</cb:entry>
  				</u:insert>
  			</u:context>
  			<job:feed stream="response" base="job" content-type="text/html">
  				<html>
  					<head>
  						<title>LivCos Clipboard</title>
  					</head>
  					<body style="text-align: center" onblur="window.close()">
  						<p>Successfully copied the selection to the clipboard!</p>
  						<div 
  							style="font-style: italic; font-size: 90%; padding: 6px 8px 2px; margin: 0 8px 20px; background-color: #F7F7CC">
  							<div style="text-align: left">"<xsl:value-of select="$s"/>"</div>
  							<div style="text-align: right; font-size: 70%;">
  								<xsl:value-of select="$t"/>
  								<xsl:text>, </xsl:text>
  								<xsl:value-of select="format-date(current-date(), '[D].[M].[Y]')"/>
  							</div>
  							<div style="text-align: right; font-size: 70%;">
  								<xsl:value-of select="$u"/><xsl:value-of select="$id"/>
  							</div>
  						</div>
  						<input type="button" value="Close" onclick="window.close()"/>
  					</body>
  				</html>
  			</job:feed>
  		</xsl:when>
  		<xsl:otherwise>
  			<job:feed stream="response" base="job" content-type="text/html">
  				<html>
  					<head>
  						<title>LivCos Clipboard</title>
  						<style type="text/css">
  							body {
  								background-color: gray;
	  							font-family: Verdana, Arial, Helvetica, sans-serif;
  							}
  							div {
  								max-width: 600px;
  								margin: 5% auto;
  								background-color: white;
  								padding: 20px 40px;
  							}
  							a {
	  							text-decoration: none;
	  							background-color: #DDDDDD;
	  							background-image: url("livcos.org/web/img/file.png");
	  							background-position: left center;
	  							background-repeat: no-repeat;
	  							padding: 2px 4px 2px 18px;
	  							font-size: 90%;
  							}
  						</style>
  					</head>
  					<body>
  						<div>
  						<h3>LivCos Clipboard Job</h3>
  						<p>This job allows to copy a text selection within the Web browser as a citation into the LivCos clipboard.</p>
  						<p>Simply drag this bookmarklet into your bookmark toolbar:</p>
  						<p>
  							<xsl:variable name="code">
  								<xsl:text>javascript:(function(){</xsl:text>
  								<xsl:text>var a=window,d=document,e=encodeURIComponent</xsl:text>
  								<xsl:text>,s=getSelection(),n=s.anchorNode;while(n){if(n.id)break;n=n.parentNode;}</xsl:text>
  								<xsl:text>var w=a.open('http://</xsl:text>
  								<xsl:variable name="url" select="$jobObject/c:request/url"/>
  								<xsl:value-of select="$url/@server"/>
  								<xsl:if test="$url/@port != '80'">:<xsl:value-of select="$url/@port"/></xsl:if>
  								<xsl:value-of select="$jobObject/c:request/@context-path"/>
  								<xsl:text>/cb.job?</xsl:text>
  								<xsl:text>u='+e(d.location)+((n)?'&amp;i='+n.id:'')+'&amp;t='+e(d.title)+'&amp;s='+e(s)</xsl:text>
  								<xsl:text>,'cb','width=420,height=230,dialog=1,alwaysRaised=1,centerscreen');</xsl:text>
  								<xsl:text>a.setTimeout(function(){w.focus()},300);</xsl:text>
  								<xsl:text>})()</xsl:text>
  							</xsl:variable>
  							<a href="{$code}">copy</a>, <a href="{$code}">+</a>,
  							<a href="{$code}">cp</a>, <a href="{$code}">cb</a>
  						</p>
  						</div>
  					</body>
  				</html>
  			</job:feed>
  		</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>
  
  <xsl:template name="createEntry">
    <xsl:param name="type"/>
  </xsl:template>
  
	<xsl:template name="build">
		<xsl:if test="not($clipboard)">
			<u:context ref="{$sessionObjId}" scope="meta">
				<u:insert select=".">
					<cb:clipboard/>
				</u:insert>
			</u:context>
		</xsl:if>
	</xsl:template>
	
</xsl:stylesheet>
